package com.zkt.subject.application.interceptor;

import cn.hutool.core.util.StrUtil;
import com.zkt.subject.common.constants.SubjectConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/23
 * @description Feign请求拦截器
 **/

@Component
public class FeignRequestInterceptor implements RequestInterceptor {
	
	@Override
	public void apply(RequestTemplate template) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (requestAttributes == null) {
			return;
		}
		
		HttpServletRequest request = requestAttributes.getRequest();
		
		String loginId = request.getHeader(SubjectConstant.LOGIN_ID);
		if (StrUtil.isNotBlank(loginId)) {
			template.header(SubjectConstant.LOGIN_ID, loginId);
		}
	}
}